package com.kmt.plaza.controller;

import com.kmt.plaza.entity.*;
import com.kmt.plaza.sevice.ProductService;
import com.kmt.plaza.vo.ResStatus;
import com.kmt.plaza.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/product")
@Api(value = "商品信息相关的接口",tags = "商品管理")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation("商品基本信息查询接口")
    @GetMapping("/detailInfo/{pid}")
    public ResultVO getProductBasicInfo(@PathVariable("pid") String pid){
        return productService.getProductBasicInfo(pid);
    }
    //查询商品列表
    @ApiOperation("查询商品列表接口")
    @GetMapping("/selectProductList")
    public ResultVO selectProductList(int pageNum,int limit){
        return productService.selectProductLists(pageNum,limit);
    }
    @ApiOperation(value = "添加商品接口")
    @PutMapping(value = "/insertProduct")
    public ResultVO insertProduct(@RequestBody Product product) {
        int result = productService.insertProduct(product);
//        Date createTime = product.getCreateTime();selectByCondition
//        Date updateTime = product.getUpdateTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String createTime1 =  sdf.format(createTime);//直接用format返回字符串
//        String updateTime1 =  sdf.format(updateTime);
//        System.out.println(createTime1+updateTime1);

        if (result > 0) {
            return new ResultVO(ResStatus.OK, "添加成功", result);
        } else {
            return new ResultVO(ResStatus.NO, "添加失败", null);
        }
    }

    @ApiOperation(value = "添加商品参数接口")
    @PutMapping(value = "/insertProductParam")
    public ResultVO insertProductParam(@RequestBody ProductParams productParams) {
        int result = productService.insertProductParam(productParams);
//        Date createTime = product.getCreateTime();
//        Date updateTime = product.getUpdateTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String createTime1 =  sdf.format(createTime);//直接用format返回字符串
//        String updateTime1 =  sdf.format(updateTime);
//        System.out.println(createTime1+updateTime1);

        if (result > 0) {
            return new ResultVO(ResStatus.OK, "添加成功", result);
        } else {
            return new ResultVO(ResStatus.NO, "添加失败", null);
        }
    }

    @ApiOperation(value = "添加商品规格接口")
    @PutMapping(value = "/insertProductSku")
    public ResultVO insertProductSku(@RequestBody ProductSku productSku) {
        int result = productService.insertProductSku(productSku);
//        Date createTime = product.getCreateTime();
//        Date updateTime = product.getUpdateTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String createTime1 =  sdf.format(createTime);//直接用format返回字符串
//        String updateTime1 =  sdf.format(updateTime);
//        System.out.println(createTime1+updateTime1);

        if (result > 0) {
            return new ResultVO(ResStatus.OK, "添加成功", result);
        } else {
            return new ResultVO(ResStatus.NO, "添加失败", null);
        }
    }

    @ApiOperation(value = "添加商品图片接口")
    @PutMapping(value = "/insertProductImg")
    public ResultVO insertProductImg(@RequestBody ProductImg productImg) {
        int result = productService.insertProductImg(productImg);
//        Date createTime = product.getCreateTime();
//        Date updateTime = product.getUpdateTime();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String createTime1 =  sdf.format(createTime);//直接用format返回字符串
//        String updateTime1 =  sdf.format(updateTime);
//        System.out.println(createTime1+updateTime1);

        if (result > 0) {
            return new ResultVO(ResStatus.OK, "添加成功", result);
        } else {
            return new ResultVO(ResStatus.NO, "添加失败", null);
        }
    }

    @ApiOperation(value = "删除商品接口")
    @DeleteMapping(value = "/deleteProduct/{productId}")
    public int deleteUser(@PathVariable String productId)
    {
        // 删除该条商品信息
        int i = productService.deleteProduct(productId);
        return i;
    }
    //修改商品
    @ApiOperation(value = "修改商品信息接口")
    @PutMapping(value = "/updateProduct")
    public ResultVO updateUserInfo(@RequestBody Product product){
        int result = productService.updateProduct(product);
        if(result==1){
            return new ResultVO(ResStatus.OK,"修改成功",result);
        }else{
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
    }
    //修改商品参数
    @ApiOperation(value = "修改商品参数信息接口")
    @PutMapping(value = "/updateProductParamInfo")
    public ResultVO updateProductParamInfo(@RequestBody ProductParams productParams){
        int result = productService.updateProductParam(productParams);
        if(result==1){
            return new ResultVO(ResStatus.OK,"修改成功",result);
        }else{
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
    }
    //修改商品规格
    @ApiOperation(value = "修改商品规格信息接口")
    @PutMapping(value = "/updateProductSkuInfo")
    public ResultVO updateProductSkuInfo(@RequestBody ProductSku productSku){
        int result = productService.updateProductSku(productSku);
        if(result==1){
            return new ResultVO(ResStatus.OK,"修改成功",result);
        }else{
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
    }
    //修改商品图片
    @ApiOperation(value = "修改商品图片信息接口")
    @PutMapping(value = "/updateProductImgInfo")
    public ResultVO updateProductImgInfo(@RequestBody ProductImg productImg){
        int result = productService.updateProductImg(productImg);
        if(result==1){
            return new ResultVO(ResStatus.OK,"修改成功",result);
        }else{
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
    }
    /**
     * 管理员按条件查询商品接口
     */
    @ApiOperation(value = "管理员按条件查询接口")
    @GetMapping(value = "/selectByCondition")
    public ResultVO selectByCondition(int pageNum,int limit,String keyWord,String productStatus,String firstClassityId){
     return productService.selectByCondition(pageNum,limit,keyWord,productStatus,firstClassityId);
    }


    @ApiOperation("根据类别查询商品接口")
    @GetMapping("/listbycid/{cid}")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页码",required = true),
            @ApiImplicitParam(dataType = "int",name = "limit", value = "每页显示条数",required = true)
    })
    public ResultVO getProductsByCategoryId(@PathVariable("cid") int cid,int pageNum,int limit){
        return productService.getProductsByCategoryId(cid,pageNum,limit);
    }


    @ApiOperation("根据类别查询商品品牌接口")
    @GetMapping("/listbrands/{cid}")
    public ResultVO getBrandsByCategoryId(@PathVariable("cid") int cid){
        return productService.listBrands(cid);
    }

    @ApiOperation("根据关键字查询商品接口")
    @GetMapping("/listbykeyword")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "keyword", value = "搜索关键字",required = true),
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页码",required = true),
            @ApiImplicitParam(dataType = "int",name = "limit", value = "每页显示条数",required = true)
    })
    public ResultVO searchProducts(String keyword,int pageNum,int limit){
        return productService.searchProduct(keyword,pageNum,limit);
    }

    @ApiOperation("根据关键字查询商品品牌接口")
    @GetMapping("/listbrands-keyword")
    @ApiImplicitParam(dataType = "string",name = "keyword", value = "搜索关键字",required = true)
    public ResultVO getBrandsByKeyword(String keyword){
        return productService.listBrands(keyword);
    }

    @GetMapping("/productOverview")
    @ApiOperation("管理员按订单状态查询接口")
    public ResultVO productOverview(){
        ResultVO resultVO = productService.productOverview();
        return resultVO;
    }

    @GetMapping("/inventoryShortage")
    @ApiOperation("管理员查询库存紧张商品接口")
    public ResultVO inventoryShortage(){
        ResultVO resultVO = productService.inventoryShortage();
        return resultVO;
    }

}
